import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.junit.Before;
import org.junit.Test;


public class MotorTest {

	Motor aMotor;

	@Before
	public void setUp() throws Exception {
		aMotor = new Motor("XX1", 100);
	}

	@Test
	public void initalized() {
		String serial = aMotor.getSerialNumber();
		assertTrue(serial.equals("XX1"));
		
		int maximumHours = aMotor.getMaximumHoursBeforeService();
		assertTrue(maximumHours == 100);
		
		int currentHours = aMotor.getCurrentHours();
		assertTrue(currentHours == 0);
	}
	
	@Test
	public void addingHoursIncrements() {
		aMotor.addHours(10);
		int hours = aMotor.getCurrentHours();
		assertTrue(hours == 10);
		
		aMotor.addHours(15);
		hours = aMotor.getCurrentHours();
		assertTrue(hours == 10 + 15);
		
		aMotor.addHours(20);
		hours = aMotor.getCurrentHours();
		assertTrue(hours == 10 + 15 + 20);
		
	}

	@Test
	public void notPastServiceTime() {
		aMotor.addHours(100 - 10);
		
		boolean isPast = aMotor.isPastTimeForService();
		
		assertTrue(isPast == false);
	}
	
	@Test
	public void gonePastService() {
		aMotor.addHours(100 + 50);
		
		boolean isPast = aMotor.isPastTimeForService();
		
		assertTrue(isPast == true);
	}
	
	@Test
	public void gonePastServiceInHours() {
		aMotor.addHours(100 + 50);
		
		int hoursPast = aMotor.getHoursPastService();
		
		assertTrue(hoursPast == 50);
	}
	
	@Test
	public void notGonePastServiceInHours() {
		aMotor.addHours(100 + -20);
		
		int hoursPast = aMotor.getHoursPastService();
		
		assertTrue(hoursPast == 0);
	}
	
	@Test
	public void equalsMatches() {
		Motor aa = new Motor("AA", 20);
		
		Motor looksLikeAA = new Motor("AA", 0);
		
		boolean matches = aa.equals(looksLikeAA);
		assertTrue(matches == true);
	}
	
	@Test
	public void equalsDoesntMatch() {
		Motor aa = new Motor("AA", 20);
		
		Motor doesntLookLikeAA = new Motor("BB", 20);
		
		boolean matches = aa.equals(doesntLookLikeAA);
		assertTrue(matches == false);
	}
	
	@Test
	public void sortOrdersLowestToHighest() {
		List<Motor> allMotors = new ArrayList<Motor>();

		Motor first = new Motor("GG", 10);
		Motor third = new Motor("HH", 20);
		Motor second = new Motor("JJ", 20);
		
		first.addHours(3);
		third.addHours(17);
		second.addHours(5);
		
		allMotors.add(first);
		allMotors.add(third);
		allMotors.add(second);
		
		Collections.sort(allMotors);

		assertTrue(allMotors.get(0) == first);
		assertTrue(allMotors.get(1) == second);
		assertTrue(allMotors.get(2) == third);
	}
}
